﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMS.Utility
{
    /// <summary>
    /// 列表字符串相等指示器
    /// </summary>
    /// <remarks>用于提供字符串列表的元素检索的定位</remarks>
    /// <history>
    /// [ZaneZeng]               2009-3-30 9:39    创建
    /// </history>
    public sealed class StringEqualityPredicator
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="StringEqualityPredicator"/>的新实例
        /// </summary>
        /// <param name="comparer">字符串比较器</param>
        /// <param name="target">要检索的目标</param>
        /// <remarks>同为Null的字符串认为相等</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="comparer"/>为Null</exception>
        /// <history>
        /// [ZaneZeng]               2009-3-30 9:40    创建
        /// </history>
        public StringEqualityPredicator( IComparer<string> comparer, string target )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( comparer, "comparer" );

            //保存参数
            this.m_Comparer = comparer;
            this.m_Target = target;
        }

        #endregion

        #region ---Public Method

        /// <summary>
        /// 判断指定的字符串是否与目标字符串相等
        /// </summary>
        /// <param name="value">要与目标字符串进行比较的字符串</param>
        /// <returns>
        /// 	<c>true</c>：指定的字符串与目标字符串相等<br/>
        /// 	<c>false</c>：指定的字符串与目标字符串不相等
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [ZaneZeng]               2009-3-30 9:45    创建
        /// </history>
        public bool Predicate( string value )
        {
            //判断目标字符串是否为Null
            if (null == this.m_Target)
            {
                //判断指定字符串是否为NULL
                return null == value;
            }

            //判断指定字符串是否为Null
            if (null == value)
            {
                //判断目标字符串是否为Null
                return null == this.m_Target;
            }

            //使用比较器进行比较
            return this.m_Comparer.Compare( value, this.m_Target ) == 0;
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 字符串比较器
        /// </summary>
        private IComparer<string> m_Comparer;

        /// <summary>
        /// 要检索的目标
        /// </summary>
        private string m_Target;

        #endregion
    }
}
